//
//  CoreDataManager.m
//  Four_Fit_New
//
//  Created by lanou on 16/7/19.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "CoreDataManager.h"
#import "REcommendScroll.h"
#import "RecScrollModel.h"
#import "RecTableModel.h"
#import "Recommend.h"
#import "EatModel.h"
#import "Eating.h"
#import "insModel.h"
#import "Inspirational.h"


@interface CoreDataManager()

@property (nonatomic,strong)NSManagedObjectContext *contect;

@end
static CoreDataManager *manager = nil;
@implementation CoreDataManager
///类方法，创建
+ (instancetype)creatCoreDataManager{
    if (manager == nil) {
        manager = [[CoreDataManager alloc]init];
    }
    return manager;
}
///懒加载
- (NSManagedObjectContext *)contect{
    if (!_contect) {
        _contect = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
        ///关联数据库
        NSManagedObjectModel *model = [[NSManagedObjectModel alloc]initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"FinModel" withExtension:@"momd"]];
        ///通过文件模型创建数据持久化助理
        NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
        ///创建文件路径
        NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"FinModel.sqlite"];
        ///关联路径
        [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:filePath] options:@{NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES} error:nil];
        
        self.contect.persistentStoreCoordinator = store;
        NSLog(@"%@",filePath);
    }
    return _contect;
}


///创建插入（轮播图）
- (void)addRecommendScrollPhotoWithModel:(RecScrollModel *)model{
    REcommendScroll *scr = [NSEntityDescription insertNewObjectForEntityForName:@"REcommendScroll" inManagedObjectContext:self.contect];
    scr.photo = model.photo;
    scr.url = model.url;
    [self.contect save:nil];
    
}
///推荐列表插入
- (void)addRecommendTabWithModel:(RecTableModel *)model{
    Recommend *rec = [NSEntityDescription insertNewObjectForEntityForName:@"Recommend" inManagedObjectContext:self.contect];
    rec.title = model.title;
    rec.url = model.url;
    rec.photo = model.photo;
    rec.content = model.content;
    [self.contect save:nil];
}
///饮食列表插入
- (void)addEatingTabWithModel:(EatModel *)model{
    
    
    Eating *eat = [NSEntityDescription insertNewObjectForEntityForName:@"Eating" inManagedObjectContext:self.contect];
    
    eat.title = model.title;
    eat.content = model.content;
    eat.url = model.url;
    eat.photo = model.photo;
    
    [self.contect save:nil];
}

///励志列表数据插入
- (void)addInspirationalTabWithModel:(insModel *)model{
    
    Inspirational *ins = [NSEntityDescription insertNewObjectForEntityForName:@"Inspirational" inManagedObjectContext:self.contect];
    ins.title = model.title;
    ins.content = model.content;
    ins.url = model.url;
    ins.photo = model.photo;
    [self.contect save:nil];
}
///查找
- (NSArray *)selectActionWithName:(NSInteger)index{
    
    ///NSString *name = [NSString string];
    if (index==0) {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"REcommendScroll"];
        NSArray *arr = [self.contect executeFetchRequest:request error:nil];
        return arr;
    }
    if (index==1) {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Recommend"];
        NSArray *arr = [self.contect executeFetchRequest:request error:nil];
        return arr;
    }
    if (index==2) {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Eating"];
        NSArray *arr = [self.contect executeFetchRequest:request error:nil];
        return arr;
    }
    if (index==3) {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Inspirational"];
        NSArray *arr = [self.contect executeFetchRequest:request error:nil];
        return arr;
    }else{
        
        return NULL;
    }
}

///删除
- (void)deleteDataWithIndex:(NSInteger)index{
    
    NSArray *goal = [self selectActionWithName:index];
    for (id model in goal) {
        [self.contect deleteObject:model];
        [self.contect save:nil];
    }
    
    
}










@end
